package cn.itcast.bos.service.base.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.AreaRepository;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.utils.ExcelUtil;
import cn.itcast.bos.utils.PinYin4jUtils;

@Service
@Transactional
public class AreaServiceImpl implements AreaService {

	@Autowired
	private AreaRepository areaRepository;

	@Override
	public Page<Area> searchArea(final Area model, Pageable pageable) {
		Specification<Area> spec = new Specification<Area>() {

			@Override
			public Predicate toPredicate(Root<Area> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				if (StringUtils.isNotBlank(model.getProvince())) {
					Predicate p1 = cb.like(root.get("province").as(String.class), "%" + model.getProvince() + "%");
					list.add(p1);
				}
				if (StringUtils.isNotBlank(model.getCity())) {
					Predicate p2 = cb.like(root.get("city").as(String.class), "%" + model.getCity() + "%");
					list.add(p2);
				}
				if (StringUtils.isNotBlank(model.getDistrict())) {
					Predicate p3 = cb.like(root.get("district").as(String.class), "%" + model.getDistrict() + "%");
					list.add(p3);
				}
				return cb.and(list.toArray(new Predicate[0]));
			}
		};
		return areaRepository.findAll(spec, pageable);
	}

	@Override
	public void saveBatch(File file, String fileFileName) {

		List<Area> list = new ArrayList<Area>();
		Sheet sheet = ExcelUtil.getExcelSheetByFile(file, fileFileName);

		// 3.读取sheet中每一行
		for (Row row : sheet) {
			// 一行数据对应一个对象
			// 跳过第一行
			if (row.getRowNum() == 0) {
				continue;
			}
			// 跳过空行
			if (row.getCell(0) == null || StringUtils.isBlank(row.getCell(0).getStringCellValue())) {
				continue;
			}

			Area area1 = new Area();
			area1.setId(row.getCell(0).getStringCellValue());
			area1.setProvince(row.getCell(1).getStringCellValue());
			area1.setCity(row.getCell(2).getStringCellValue());
			area1.setDistrict(row.getCell(3).getStringCellValue());
			area1.setPostcode(row.getCell(4).getStringCellValue());

			String province = area1.getProvince();
			String city = area1.getCity();
			String district = area1.getDistrict();
			province = province.substring(0, province.length() - 1);
			city = city.substring(0, city.length() - 1);
			district = district.substring(0, district.length() - 1);

			String[] headArray = PinYin4jUtils.getHeadByString(province + city + district);
			StringBuffer buffer = new StringBuffer();
			for (String headStr : headArray) {
				buffer.append(headStr);
			}
			String shortcode = buffer.toString();
			area1.setShortcode(shortcode);
			String citycode = PinYin4jUtils.hanziToPinyin(city, "");
			area1.setCitycode(citycode);

			list.add(area1);
		}
		areaRepository.save(list);
	}

}
